home *** CD-ROM | disk | FTP | other *** search
- Path: chronicle.mti.sgi.com!austern
- From: David Byrden <Goyra@iol.ie>
- Newsgroups: comp.std.c++
- Subject: Re: Function object operator() is too anonymous.
- Date: 28 Mar 1996 17:35:41 PST
- Organization: Ireland On-Line
- Approved: austern@isolde.mti.sgi.com
- Message-ID: <4jeupm$v60@nuacht.iol.ie>
- References: <4jerpo$bvb@ugress.uib.no>
- NNTP-Posting-Host: isolde.mti.sgi.com
- X-Original-Date: 28 Mar 1996 21:02:46 GMT
- X-Mailer: Mozilla 1.22KIT (Windows; I; 16bit)
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBVAwUBMVs+bky4NqrwXLNJAQFMxgH+MIVbVqqUCW+R5AhZ/MKFQ5r4CPyXS51d
- b4cy3e/3wPXPgBXBff9DNV9tqhb4zzSKEnYY3hHl7D1V0ECY5hnMEQ==
- =dEkm
- Originator: austern@isolde.mti.sgi.com
-
- boukanov@sentef1.fi.uib.no (Igor Boukanov) wrote:
-
- > I noticed that that it impossible to write Function object class
- >(DWP 20.3) that can be used for example as Arithmetic operation object
- >and Predicate object in the same time, because both require operator()
- >that can not be overloaded!
-
- >So why do not have instead of one anonymous
- >operator() a set of function obect methods like
- >unary_operation, binary_operation, predicate, comparison
- >and each function from algorithm.h will call correspondent method.
- >For example, find_if will call predicate method of function object and
- >transform will call unary_operation or binary_operation
- >instead of operator().
- >
- >In this case one can write:
- >
- >class F {
- > ...
- > bool predicate(int); // instead of bool operator()(int);
- > int unary_operation(int); // instead of int operator()(int);
- > int binary_operation(int, int); // instead of int operator()(int, int);
- > bool comparison(int, int); // instead of bool operator()(int, int);
- >};
- >
- >and somewhere use F:
- >
- >vector<int> v1, v2;
- >F f;
- >
- >...
- >std::find_if(v1.begin(), v1.end(), f);
- > // f.predicate(int) will be called
- >std::transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), f);
- > // f.binary_operation(int, int) will be called
- >std::equal(v1.begin(), v1.end(), v2.begin(), f);
- > // f.comparison(int, int) will be called
- >
-
-
- Igor; perhaps you are not quite clear about how functors are used.
-
- They are interchangeable with ordinary function addresses; this would no
- longer be possible if you were to replace operator() with a member
- function called, for example, unary_operation, because function addresses
- do not have it. For example;
-
- bool ordinary_function( int ) ;
-
- std::find_if(v1.begin(), v1.end(), ordinary_function ) ;
- // works if the algorigthm calls operator()()
- // fails if the algorithm calls unary_operation()
-
-
-
- Also, there is no need to use a single functor class F in these two
- cases, as you did;
-
- F f ;
- std::transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), f );
- std::equal(v1.begin(), v1.end(), v2.begin(), f );
-
- you would normally use different functors, depending on what you want the
- algorithm to do.
-
- The library provides many interchangeable functors, and you can build a
- collection of your own ones. There is no requirement to use only one
- functor in all cases.
-
-
-
- The purpose of a functor is to be a "function with attached data", as
- this example makes clear;
-
- find_if( v.begin(), v.end(), IsEqualTo(6) ) ;
-
- The functor is effectively a function which I have customised by attaching
- the number 6 to it. So, I am not obliged to write a whole set of ordinary
- functions like these;
-
- bool IsEqualTo6( int ) ;
- bool IsEqualTo7( int ) ;
- bool IsEqualTo8( int ) ;
- ..................
-
-
- David
- ---
- [ comp.std.c++ is moderated. To submit articles: Try just posting with your
- newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
- comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
- Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
- Comments? mailto:std-c++-request@ncar.ucar.edu
- ]
-